//
//  SHTextView.m
//
//  Created by 周成龙
//

#import "SKTextView.h"

#define Font(a)     [UIFont systemFontOfSize:a]

@interface SKTextView()

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

static NSString * const kPattern   = @"#[\u4e00-\u9fa5a-zA-Z0-9]{1,30}[:|\\s]{0,1}";
@implementation SKTextView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChange:) name:UITextViewTextDidChangeNotification object:self];
        
        float left = 5,top = 9,hegiht = 14;
        
        UILabel*  placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(left, top, CGRectGetWidth(self.frame)-2*left, hegiht)];
        self.placeholderLabel = placeholderLabel;
        placeholderLabel.backgroundColor = [UIColor clearColor];
        placeholderLabel.numberOfLines = 1;
        placeholderLabel.textColor = self.placeholderColor;
        placeholderLabel.text = self.placeholder;
        placeholderLabel.userInteractionEnabled = NO;
        [self addSubview:placeholderLabel];
    }
    return self;
}

- (void)didChange:(NSNotification *)notification {
    NSRange lastRange = self.selectedRange;
    NSString *text = self.text;
    if ([text length]> 0) {
        self.placeholder = @"";
        NSError *error;
        NSRegularExpression *regex = [NSRegularExpression
                                      regularExpressionWithPattern:kPattern
                                      options:0
                                      error:&error];
        NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:text];
        if (!error) { // 如果没有错误
            NSArray *array1 = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];
//            NSLog(@"%ld",(long)[array1 count]);
            if ([array1 count]) {
                for (NSTextCheckingResult *objc in array1) {
                    NSRange range = objc.range;
                    //colorWithHexString:@"0x008ff3"
                    [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(range.location, range.length)];
                    [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(range.location, range.length - 1)];
//                    NSLog(@"%@",NSStringFromRange(objc.range));
                }
                self.font = Font(15.0f);
                [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(0, text.length)];
                self.attributedText = attribute;
            }else {
                [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, text.length)];
                [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(0, text.length)];
                self.attributedText = attribute;
            }
            [self setSelectedRange:lastRange];
        } else { // 如果有错误，则把错误打印出来
//            NSLog(@"error - %@", error);
        }
    } else {
        self.placeholderLabel.text  = self.placeholder;
    }
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    self.placeholderLabel.textColor = placeholderColor;
}
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    self.placeholderLabel.text = placeholder;
    [self.placeholderLabel sizeToFit];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_placeholderLabel removeFromSuperview];
}
@end






